<template>
  <div class="register-container">
    <el-card class="register-card">
      <h2 class="register-title">酒店管理系统注册</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" :min="1" :max="120" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-radio-group v-model="form.roleId">
            <el-radio :label="1">普通用户</el-radio>
            <el-radio :label="2">酒店商家</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="酒店名称" prop="hotelName" v-if="form.roleId === 2">
          <el-input v-model="form.hotelName" placeholder="请输入酒店名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onRegister" :loading="loading" style="width:100%">注册</el-button>
        </el-form-item>
        <div class="login-link">
          已有账号？<el-link type="primary" @click="goToLogin">去登录</el-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '@/api/user'
import type { FormRules, FormInstance } from 'element-plus'

const router = useRouter()
const formRef = ref<FormInstance>()

const form = reactive({
  email: '',
  password: '',
  confirmPassword: '',
  age: 18,
  gender: 1, // 默认男性
  roleId: 1, // 默认普通用户
  hotelName: ''
})

// 自定义验证规则 - 确认密码
const validateConfirmPassword = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== form.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

// 自定义验证规则 - 酒店名称
const validateHotelName = (rule: any, value: string, callback: any) => {
  if (form.roleId === 2 && !value) {
    callback(new Error('酒店商家必须填写酒店名称'))
  } else {
    callback()
  }
}

const rules: FormRules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  age: [
    { required: true, message: '请输入年龄', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  roleId: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ],
  hotelName: [
    { validator: validateHotelName, trigger: 'blur' }
  ]
}

// 监听roleId变化，重新验证hotelName
watch(() => form.roleId, () => {
  if (formRef.value) {
    formRef.value.validateField('hotelName')
  }
})

const loading = ref(false)

const onRegister = () => {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    
    // 如果是普通用户，不需要酒店名称
    const params = { ...form }
    if (params.roleId === 1) {
      params.hotelName = undefined
    }
    
    // 删除确认密码字段
    delete params.confirmPassword
    
    loading.value = true
    register(params).then((res: any) => {
      const result = res.data
      if (result.code === 200 && result.data) {
        ElMessage.success('注册成功，请登录')
        router.push('/login')
      } else {
        ElMessage.error(result.msg || '注册失败')
      }
      loading.value = false
    }).catch((error) => {
      console.error('注册错误:', error)
      loading.value = false
      ElMessage.error('注册失败，请稍后重试')
    })
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f9f6f2;
  z-index: 10;
}
.register-card {
  width: 450px;
  padding: 40px 30px 20px 30px;
}
.register-title {
  text-align: center;
  margin-bottom: 30px;
}
.login-link {
  text-align: center;
  margin-top: 15px;
}
</style> 